package com.yh.csx.crm.task;

import com.github.pagehelper.PageHelper;
import com.yh.csx.crm.dao.CustomerMapper;
import com.yh.csx.crm.qo.SimpleCustomerQo;
import com.yh.csx.crm.vo.CustomerSimpleVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.function.Consumer;

/**
 * @author Huang Zhaoping
 * @date 2019/3/21
 */
public abstract class AbstractCustomerSyncTask {

    @Autowired
    private CustomerMapper customerMapper;

    protected final void syncAllCustomer(SimpleCustomerQo qo, Consumer<List<CustomerSimpleVo>> consumer) {
        int batchSize = qo.getBatchSize();
        for (int pageNum = 1; ; pageNum++) {
            PageHelper.startPage(pageNum, batchSize, false);
            List<CustomerSimpleVo> customers = customerMapper.selectCustomerSimpleList(qo);
            if (customers.size() > 0) {
                consumer.accept(customers);
            }
            if (customers.size() < batchSize) {
                break;
            }
        }
    }
}
